package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.BeanHelper;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.template.IDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;

import java.util.HashMap;
import java.util.Map;

public class JavaServiceData implements IDataLoador {

    public static String getClassName(String tableName) {
        return "Dao" + TableHelper.tableNameToClassName(tableName) + "Service";
    }

    public static String getFullClassName(String classPath, String tableName) {
        return classPath + "." + getClassName(tableName);
    }

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/service.java.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return getClassName(tableName) + ".java";
    }

    @Override
    public String getFileOutPath() {
        return ParamConfig.getInstance().getServiceOutPath();
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();

        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("tableComment", tableVO.readTableComment());
        map.put("tableCommentFull", tableVO.getTableComment());
        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        map.put("entityOutPath", getFileOutPath());
        map.put("entityClassName", getClassName(tableName));

        String baseFullClassName = ParamConfig.getInstance().getServiceBaseClass();
        String baseClassName = BeanHelper.getBeanClassName(ParamConfig.getInstance().getServiceBaseClass());
        map.put("baseFullClassName", baseFullClassName);
        map.put("baseClassName", baseClassName);
        if (TableVO.TABLE_TYPE_DEL == tableVO.getTableType()) {
            baseFullClassName = baseFullClassName.replace(baseClassName, "BaseAllService");
            map.put("baseFullClassName", baseFullClassName);
            map.put("baseClassName", "BaseAllService");
        } else {
            baseFullClassName = baseFullClassName.replace(baseClassName, "BaseEditService");
            map.put("baseFullClassName", baseFullClassName);
            map.put("baseClassName", "BaseEditService");
        }

        map.put("dtoClassName", JavaDtoData.getClassName(tableName));
        map.put("dtoFullClassName", JavaDtoData.getFullClassName(ParamConfig.getInstance().getDtoOutPath(), tableName));

        map.put("queryClassName", JavaQueryData.getClassName(tableName));
        map.put("queryFullClassName", JavaQueryData.getFullClassName(ParamConfig.getInstance().getQueryOutPath(), tableName));

        map.put("pk", tableVO.getPk());

        return map;
    }

}
